<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>售后管理 - 编辑售后订单 | 电动车销售系统</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/aftersalesadmin/editaftersale.css">
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">售后管理 - 编辑售后订单</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="d-none d-md-inline">${currentUser.name}</span>
                    <div class="badge bg-light text-dark">
                        <i class="bi bi-person-circle me-1"></i> 售后管理员
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-gear fs-5"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/change_password.jsp"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/LogoutServlet"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="container mb-5">
    <div class="row justify-content-center">
        <div class="col-lg-9">
            <div class="dashboard-card shadow">
                <div class="card-header">
                    <div>
                        <h5 class="card-title"><i class="bi bi-tools me-2 text-primary"></i>编辑售后订单 #${afterSales.id}</h5>
                        <p class="card-subtitle">修改售后订单信息</p>
                    </div>
                </div>

                <div class="card-body">
                    <c:if test="${not empty errorMsg}">
                        <div class="alert-danger mb-4">
                            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMsg}
                        </div>
                    </c:if>

                    <c:if test="${empty afterSales and empty errorMsg}">
                        <div class="alert-danger mb-4">
                            <i class="bi bi-exclamation-triangle me-2"></i>
                            无法加载售后订单数据，请<a href="${pageContext.request.contextPath}/afterSalesAdmin?action=list">返回列表</a>重试。
                        </div>
                    </c:if>

                    <c:if test="${not empty afterSales}">
                        <div class="info-box">
                            <p><i class="bi bi-info-circle"></i> 修改售后订单信息后，请确保所有数据准确无误</p>
                            <p><i class="bi bi-info-circle"></i> 状态更改后，系统将自动通知相关人员</p>
                        </div>

                        <div class="order-summary mb-4">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="mb-0"><i class="bi bi-card-checklist me-2"></i>订单摘要</h5>
                                <span class="status-badge status-${afterSales.status == 0 ? 'pending' : afterSales.status == 1 ? 'processing' : 'resolved'}">
                                    <c:choose>
                                        <c:when test="${afterSales.status == 0}">未处理</c:when>
                                        <c:when test="${afterSales.status == 1}">处理中</c:when>
                                        <c:when test="${afterSales.status == 2}">已解决</c:when>
                                    </c:choose>
                                </span>
                            </div>

                            <div class="summary-item">
                                <span class="summary-label">创建时间:</span>
                                <span class="summary-value">
                                    <fmt:formatDate value="${afterSales.createTime}" pattern="yyyy-MM-dd HH:mm" />
                                </span>
                            </div>
                        </div>

                        <form action="${pageContext.request.contextPath}/afterSalesAdmin?action=update" method="post" class="needs-validation" novalidate>
                            <input type="hidden" name="id" value="${afterSales.id}">

                            <div class="form-section">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="section-icon">
                                        <i class="bi bi-cart"></i>
                                    </div>
                                    <h5 class="form-section-title m-0">订单信息</h5>
                                </div>

                                <div class="row">
                                    <div class="col-md-6 form-group">
                                        <label for="saleOrderId" class="form-label required-label">关联销售订单ID</label>
                                        <input type="number" class="form-control form-control-lg"
                                               id="saleOrderId" name="saleOrderId"
                                               value="${afterSales.saleOrderId}" required>
                                        <div class="invalid-feedback">
                                            请输入关联销售订单ID
                                        </div>
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label for="userId" class="form-label required-label">处理用户ID</label>
                                        <input type="number" class="form-control form-control-lg"
                                               id="userId" name="userId"
                                               value="${afterSales.userId}" required>
                                        <div class="invalid-feedback">
                                            请输入处理用户ID
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="form-section">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="section-icon">
                                        <i class="bi bi-clipboard"></i>
                                    </div>
                                    <h5 class="form-section-title m-0">问题详情</h5>
                                </div>

                                <div class="form-group">
                                    <label for="problem" class="form-label required-label">问题描述</label>
                                    <textarea class="form-control form-textarea"
                                              id="problem" name="problem" rows="4" required>${afterSales.problem}</textarea>
                                    <div class="invalid-feedback">
                                        请输入问题描述
                                    </div>
                                </div>
                            </div>

                            <div class="form-section">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="section-icon">
                                        <i class="bi bi-gear"></i>
                                    </div>
                                    <h5 class="form-section-title m-0">状态信息</h5>
                                </div>

                                <div class="row">
                                    <div class="col-md-6 form-group">
                                        <label for="status" class="form-label required-label">订单状态</label>
                                        <select class="form-select form-select-lg" id="status" name="status" required>
                                            <option value="0" ${afterSales.status == 0 ? "selected" : ""}>未处理</option>
                                            <option value="1" ${afterSales.status == 1 ? "selected" : ""}>处理中</option>
                                            <option value="2" ${afterSales.status == 2 ? "selected" : ""}>已解决</option>
                                        </select>
                                        <div class="invalid-feedback">
                                            请选择订单状态
                                        </div>
                                    </div>
                                    <div class="col-md-6 form-group">
                                        <label for="createTime" class="form-label required-label">创建时间</label>
                                        <input type="datetime-local" class="form-control form-control-lg"
                                               id="createTime" name="createTime"
                                               value="<fmt:formatDate value='${afterSales.createTime}' pattern='yyyy-MM-dd\'T\'HH:mm' />" required>
                                        <div class="invalid-feedback">
                                            请选择创建时间
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="action-buttons">
                                <button type="submit" class="btn btn-submit">
                                    <i class="bi bi-check-circle me-1"></i> 更新订单
                                </button>
                                <a href="${pageContext.request.contextPath}/afterSalesAdmin?action=list" class="btn btn-cancel">
                                    <i class="bi bi-x-circle me-1"></i> 取消返回
                                </a>
                            </div>
                        </form>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 表单验证
    (function () {
        'use strict'

        var forms = document.querySelectorAll('.needs-validation')

        Array.prototype.slice.call(forms)
            .forEach(function (form) {
                form.addEventListener('submit', function (event) {
                    if (!form.checkValidity()) {
                        event.preventDefault()
                        event.stopPropagation()
                    }

                    form.classList.add('was-validated')
                }, false)
            })
    })()

    // 状态更改提示
    document.getElementById('status').addEventListener('change', function() {
        const status = this.value;
        if (status == 2) {
            alert('将状态改为"已解决"后，系统将自动通知客户处理结果！');
        }
    });
</script>
</body>
</html>